function validatePostCode(postcode){
	var validPostCode = checkPostCode(postcode);
	return validPostCode;
}

function clearArray(array){
	while(array.length > 0){
		array.pop();
	}
}

function validateField(id, validationTypes){
	var field = $("#"+id);
	var error = false;
	var message = "";
	var inputLabel = getLabelOfInput(id);
	var label = inputLabel.text().replace(":", "");
	var testRegex = function(regexp, test){
		return regexp.test(test);
	}
	var isEmpty = function(field){
		return field.val().trim() === "";
	}
	for(var i = 0; i<validationTypes.length; i++){
		switch(validationTypes[i]){
			case "empty":
				if(field.val().trim() === ""){
					error = true;
					message += "\n-The field "+label+" cannot be empty";
				}
				break;
			case "number":
				var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/;
				if(!isEmpty(field) && !testRegex(regexp,field.val())){
					error = true;
					message += "\n-The field "+label+" must be a valid number"
				}
				break;
			case "email":
				var regexp = /.{1,}.@.{1,}/;
				if(!isEmpty(field) && !testRegex(regexp,field.val())){
					error = true;
					message += "\n-The field "+label+" must contain a valid email";
				}
				break;
			case "textonly":
				var regexp = /^[a-zA-Z ]+$/;
				if(!isEmpty(field) && !testRegex(regexp,field.val())){
					error = true;
					message += "\n-The field "+label+" has invalid characters";
				}
				break;
			case "postcode":
				if(!validatePostCode(field.val())){
					error = true;
					message += "\n-The field "+label+" must contain a valid postcode";
				}
				break;
			case "dateOfBirth":
			debugger;
				if(!isEmpty(field) && !validateDateOfBirth(field.val())){
					error = true;
					message += "\n-You must be 18 or older.";
				}
				break;
		}
	}
	return message;
}


function getLabelOfInput(id){
	return $("label[for="+id+"]");
}

function parseDate(dateString){
	return Date.parse(dateString);
}

function validateDateOfBirth(dateString){
	var date = new Date(parseDate(dateString));
	var today = new Date();
	var difYear = today.getFullYear() - date.getFullYear();
	if(difYear >= 18){
		return true;
	}
	return false;
}

function concatArray(str, array){
	if(!str){
		str = "";
	}
	for(var i = 0; i<array.length; i++){
		str += array[i];
	}
	return str;
}

function getCurrentURL(){
	return document.URL;
}

function getURL(page){
	var urlArray = getCurrentURL().split("/");
	urlArray[urlArray.length - 1] = page;
	var url = urlArray.join("/");
	return url;
}

function configureLoginLayer(r)
{
	$("#loginLayerWrapper").empty();
	$("#loginLayerWrapper").append(r.htmlString);
	showLoginLayer();
	$(".loginLayerContentsBox").css("zIndex", "101");
	$("#loginLayer").on("click", function(){
		hideLoginLayer();
	});
	$("#loginLayer > div > div").on("click", function(e){e.stopPropagation()});
}

function configureListingAndProfileLayer(r)
{
	$("#profileAndListingContainer").empty();
	$("#profileAndListingContainer").append(r.htmlString);
}

function showLoginLayer(){
	document.getElementById("loginLayer").style.opacity = "1";
	document.getElementById("loginLayer").style.zIndex = "100";
	document.getElementById("loginLayer").style.display = "table";
}

function hideLoginLayer(){
	$("#loginLayer").css("opacity", "0");
	$("#loginLayer").css("zIndex", "-1");
	$("#loginLayer").css("display", "none");
}

function dontSubmit(){
	debugger;
	return false;
}

function showLoadingLayer(){
	$("#loadingLayer").css("opacity", "1");
	$("#loadingLayer").css("zIndex", "999");
}

function hideLoadingLayer(){
	$("#loadingLayer").css("opacity", "0");
	$("#loadingLayer").css("zIndex", "-1");
}

function showHideElement(elements, show, showClass, hideClass)
{
	if(!showClass){
		showClass = "showClass";	
	}
	if(!hideClass){
		hideClass = "hideClass";
	}
	var classToRemove = hideClass;
	var classToAdd = showClass;

	if(!show)
	{
		classToRemove = showClass;
		classToAdd = hideClass;
	}
	for(var i = 0; i<elements.length; i++)
	{
		var el = elements[i];
		if(typeof el == "string"){
			el = $("#"+el);
		}
		el.removeClass(classToRemove);
		el.addClass(classToAdd);
	}	
}